package com.yilin.platform.controller.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.annotation.AbnormalVerify;
import com.yilin.platform.base_core.commons.annotation.Explain;
import com.yilin.platform.base_core.commons.annotation.NotNull;
import com.yilin.platform.base_core.commons.annotation.SkipLoginVerify;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.base_core.tools.SMSUtil;
import com.yilin.platform.entity.enums.user.UserType;
import com.yilin.platform.entity.message.SmsRecord;
import com.yilin.platform.service.message.ISmsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 短信控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/26
 */
@RestController
@RequestMapping("/message/sms/")
public class SmsController extends BaseController {

    @Resource
    ISmsService smsService;

    /**
     * 获取短信发送记录 分页
     */
    @PostMapping(value = "getSmsPage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<SmsRecord>> getSmsPage(SmsRecord query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<SmsRecord> pageData = smsService.page(new PageData<>(page, limit), QueryUtil.andQueryWrapper(new QueryWrapper<>(), query));
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 根据日期删除消息记录(真实)
     */
    @PostMapping(value = "smsDeleteByDate")
    @NotNull({"startDate", "endDate"})
    public ReturnData<Object> smsDeleteByDate(@Explain("开始日期") String startDate, @Explain("结束日期") String endDate) {
        if (startDate == null || startDate.isEmpty() || endDate == null || endDate.isEmpty()) {
            return jsonView(false, "时间不能为空");
        }
        smsService.deleteByDate(startDate, endDate);
        return jsonView(true, "删除记录成功");
    }

    /**
     * 批量删除短信记录(真实)
     */
    @PostMapping(value = "smsDeleteByIds")
    @NotNull("ids")
    public ReturnData<Object> smsDeleteByIds(@Explain("ID数组") String[] ids) {
        if (ids == null || ids.length == 0) {
            return jsonView(false, "请选择需要删除的数据");
        }
        smsService.removeByIds(Arrays.asList(ids));
        return jsonView(true, "删除消息成功");
    }

    /**
     * 发送验证码消息
     */
    @PostMapping(value = "sendCodeMessage")
    @NotNull("msgType")
    @SkipLoginVerify
    @AbnormalVerify(maxCount = 1)
    public ReturnData<Map<String, Object>> sendCodeMessage(String platId,String productCode, String phone, String key, @Explain("验证码类型") SMSUtil.MsgType msgType) {
        if (StringUtil.isEmpty(key) && StringUtil.isEmpty(phone)) return jsonView(false, "手机号或发送标识不能为空!");
        return smsService.sendCodeMessage(platId, productCode, phone, msgType);
    }

    /**
     * 发送公共短信
     * 安全防护：需要登录
     */
    @PostMapping(value = "sendPhoneMessage")
    @NotNull({"phone", "message"})
    public ReturnData<Object> sendPhoneMessage( @Explain("手机号") String phone, @Explain("短信内容") String message) {
        String platId=getLoginUser().getSuperiorPlatId();//原代码 getLoginUser().getSuperiorPlatId()//如果是平台会报错
        if(getLoginUser().getUserType()== UserType.platformUser){
            platId=getLoginUser().getOrganizeId();
        }
        smsService.saveSendMessage(platId, null, phone, message, null, null, null);
        return jsonView(true, "发送成功");
    }
}
